package com.ywy.templates;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywy.common.templates.DirectiveHandler;
import com.ywy.common.templates.TemplateDirective;
import com.ywy.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 置顶博客文章列表自定义标签
 *
 * @author ywy
 * @date 2021/12/24 18:40
 */
@Component
public class PostsTemplate extends TemplateDirective {
    @Autowired
    private PostService postService;

    @Override
    public String getName() {
        return "posts";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Integer categoryId = handler.getInteger("categoryId");
        Integer level = handler.getInteger("level");
        Integer pn = handler.getInteger("pn", 1);
        Integer size = handler.getInteger("size", 10);
        Page page = new Page(pn, size);
        // 分页查询博客文章信息
        IPage results = postService.listByPage(page, categoryId, null, level, null, "create_time");
        handler.put(RESULTS, results).render();
    }
}
